package com.getpebble.android.common.core.trace;

import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import java.util.HashMap;
import java.util.Map;

public class Trace {

    enum Level {
        ASSERT,
        ERROR,
        WARNING,
        INFO,
        DEBUG,
        VERBOSE
    }

    public static void crash(String tag, String details) {
        sendToAll(Level.ASSERT, tag, details, null);
    }

    public static void error(String tag, String details) {
        sendToLogs(Level.ERROR, tag, details, null);
    }

    public static void error(String tag, String details, Throwable throwable) {
        sendToLogs(Level.ERROR, tag, details, throwable);
    }

    public static void error(String tag, Throwable throwable) {
        sendToLogs(Level.ERROR, tag, throwable, null);
    }

    public static void warning(String tag, String details) {
        sendToLogs(Level.WARNING, tag, details, null);
    }

    public static void warning(String tag, String details, Throwable throwable) {
        sendToLogs(Level.WARNING, tag, details, throwable);
    }

    public static void info(String tag, String details) {
        sendToLogs(Level.INFO, tag, details, null);
    }

    public static void info(String tag, String details, Throwable throwable) {
        sendToLogs(Level.INFO, tag, details, throwable);
    }

    public static void info(String tag, Throwable throwable) {
        sendToLogs(Level.INFO, tag, throwable, null);
    }

    public static void debug(String tag, String details) {
        sendToLogs(Level.DEBUG, tag, details, null);
    }

    public static void debug(String tag, String details, Throwable throwable) {
        sendToLogs(Level.DEBUG, tag, details, throwable);
    }

    public static void debug(String tag, Throwable throwable) {
        sendToLogs(Level.DEBUG, tag, throwable, null);
    }

    public static void verbose(String tag, String details) {
        sendToLogs(Level.VERBOSE, tag, details, null);
    }

    public static void verbose(String tag, String details, Throwable throwable) {
        sendToLogs(Level.VERBOSE, tag, details, throwable);
    }

    private static void sendToAll(Level level, String tag, String details, Throwable throwable) {
        Map<String, Object> detailMap = new HashMap(1);
        detailMap.put("log_message", details);
        sendToAnalytics(level, tag, throwable, detailMap);
        if (level == Level.ASSERT) {
            flushAnalytics();
        }
        sendToLogs(level, tag, details, throwable);
    }

    private static void sendToLogs(Level level, String tag, String details, Throwable throwable) {
        AndroidLogger.log(level, tag, details, throwable);
    }

    private static void sendToLogs(Level level, String tag, Throwable throwable, Map<String, Object> details) {
        if (details == null) {
            AndroidLogger.log(level, tag, "", throwable);
            return;
        }
        StringBuilder detailBuilder = new StringBuilder("\n");
        for (String key : details.keySet()) {
            detailBuilder.append('\"').append(key).append("\" = ").append(details.get(key)).append('\n');
        }
        detailBuilder.append('\n');
        AndroidLogger.log(level, tag, detailBuilder.toString(), throwable);
    }

    private static void sendToAnalytics(Level level, String tag, Throwable throwable, Map<String, Object> details) {
        MobileAppBehavior.logMobileAppDebug(throwable, tag, details);
    }

    private static void flushAnalytics() {
        AnalyticsLogger.uploadAnalytics();
    }
}
